package com.hanwei.uap.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.List;
import java.util.Map;

/**
 * @Author:zhaoam
 * @Description:
 * @Date: 2017/9/14 18:45.
 */
public class JsonUtil {

    private static final SerializeConfig config;
    private static String dateFormat = "yyyy-MM-dd HH:mm:ss";
    private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue, // 输出空置字段
            SerializerFeature.WriteNullListAsEmpty, // list字段如果为null，输出为[]，而不是null
            SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null，输出为0，而不是null
            SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null，输出为false，而不是null
            SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null，输出为""，而不是null
    };

    /**
     * ISO-8601日期格式
     yyyy-MM-dd
     yyyy-MM-dd HH:mm:ss
     yyyy-MM-dd HH:mm:ss.SSS
     毫秒数字
     毫秒数字字符串
     .NET JSON日期格式
     */
    static {
        // 修改全局的全局日期格式
//        JSON.DEFFAULT_DATE_FORMAT = dateFormat;
        config = new SerializeConfig();
//        config.put(java.util.Date.class, new SimpleDateFormatSerializer(dateFormat));
//        config.put(java.sql.Date.class, new SimpleDateFormatSerializer(dateFormat));
        config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
        config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
    }

    /**
     * 日期格式：yyyy-MM-dd HH:mm:ss
     * @param object
     * @return
     */
    public static String toJSON(Object object) {
        return toJSON(object,true);
    }

    /**
     *
     * @param object
     * @param writeDateUseDateFormat
     *   true:日期格式为yyyy-MM-dd HH:mm:ss,false:毫秒时间戳
     * @return
     */
    public static String toJSON(Object object, boolean writeDateUseDateFormat) {
        if (writeDateUseDateFormat)
            return JSON.toJSONString(object, SerializerFeature.WriteDateUseDateFormat);
        else
            return JSON.toJSONString(object);
    }

    public static String toJSONStringWithDateFormat(Object object, String dateFormat) {
        return JSON.toJSONStringWithDateFormat(object, dateFormat);
    }

    public static String toJSONStringUseISO8601DateFormat(Object object) {
        return JSON.toJSONString(object, SerializerFeature.UseISO8601DateFormat);
    }

    /**
     * 使用和json-lib兼容的日期输出格式
     * @param object
     * @return
     */
    public static String toJSONString(Object object) {
        return JSON.toJSONString(object, config, features);
    }

    /**
     * 使用和json-lib兼容的日期输出格式
     * @param object
     * @return
     */
    public static String toJSONStringNoFeatures(Object object) {
        return JSON.toJSONString(object, config);
    }

    public static JSONObject toJSONObject(String json) {
        return JSON.parseObject(json);
    }

    public static JSONArray toJSONArray(String json) {
        return JSON.parseArray(json);
    }

    public static <T> T toBean(String text, Class<T> clazz) {
        return JSON.parseObject(text, clazz);
    }

    public static <T> List<T> toBeanList(String text, Class<T> clazz) {
        return JSON.parseArray(text, clazz);
    }

    public static List<String> toStringList(String text) {
        return JSON.parseArray(text, String.class);
    }

    public static List<Map<String, Object>> toMapList(String text) {
        return JSON.parseObject(text, new TypeReference<List<Map<String, Object>>>() {
        });
    }

    /**
     * 将网络请求下来的数据用fastjson处理空的情况，并将时间戳转化为标准时间格式
     *
     * @param result
     * @return
     */
    public static String dealResponseResult(String result) {
        result = JSONObject.toJSONString(result,
                SerializerFeature.WriteClassName,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteEnumUsingToString,
                SerializerFeature.WriteSlashAsSpecial);
        return result;
    }
}
